/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package an_sim;

import java.awt.BorderLayout;
import java.util.*;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;

/**
 *
 * @author Karcsi
 */
public class tableView extends View {
    public String [] titles;
    public String [][] content;

    public tableView(FullNetwork fn) {
        super(fn);
        fn.fullSimulate(true, null, null);
        titles=new String [] {"ID", "Hierarchie Level", "Upper Member", "Lower Members", "Members", "Time"};
        int tableRow=0;
        List list=listsByLevel();
        this.content=new String[fn.getVirtualNetworkList().size()][6];
        Iterator<List> listIt=list.iterator();
        while(listIt.hasNext()){
            Iterator<VirtualNetwork> levelIt=listIt.next().iterator();
            while(levelIt.hasNext()){
                VirtualNetwork temp=levelIt.next();

                content[tableRow][0]=Integer.toString(temp.getSuperNode().getID());
                content[tableRow][1]=Integer.toString(temp.getHierarchyLevel());
                content[tableRow][2]=Integer.toString(temp.getGatewayIdUp());
                Iterator<Integer> tIt=temp.getGatewayIdDown().iterator();
                String stringTemp="";
                while(tIt.hasNext()){
                    stringTemp+=Integer.toString(tIt.next())+"; ";
                }
                content[tableRow][3]=stringTemp;

                Iterator<Node> mIt=temp.getMembers().iterator();
                stringTemp="";
                while(mIt.hasNext()){
                    stringTemp+=Integer.toString(mIt.next().getID())+"; ";
                    //System.out.print(stringTemp + "s");
                }
                
                content[tableRow][4]=stringTemp;

                content[tableRow][5]=Double.toString(temp.getTime().getSumTime());
                tableRow++;
            }
            
        }
    }

    private List listsByLevel() {
        //Annyi listára bontás, ahány szint van:
        int maxLevel = 0;
        Iterator<VirtualNetwork> it = this.fn.getVirtualNetworkList().iterator();
        while (it.hasNext()) {
            int temp = it.next().getHierarchyLevel();
            if (temp > maxLevel) {
                maxLevel = temp+1;
            }
        }
        List listsByLevel = new ArrayList();
        for (int i = 0; i < maxLevel; i++) {
            List<VirtualNetwork> temp = new ArrayList<VirtualNetwork>();
            Iterator<VirtualNetwork> tempIt = this.fn.getVirtualNetworkList().iterator();
            while (tempIt.hasNext()) {
                VirtualNetwork tempItTemp = tempIt.next();
                if (tempItTemp.getHierarchyLevel() == i) {
                    temp.add(tempItTemp);
                }
            }
            listsByLevel.add(temp);
        }
        return listsByLevel;
    }

//    private void createContent(){
//        int tableRow=0;
//        List list=listsByLevel();
//        this.content=new String[fn.getVirtualNetworkList().size()][5];
//        Iterator<List> listIt=list.iterator();
//        while(listIt.hasNext()){
//            Iterator<VirtualNetwork> levelIt=listIt.next().iterator();
//            while(levelIt.hasNext()){
//                VirtualNetwork temp=levelIt.next();
//
//                content[tableRow][0]=Integer.toString(temp.getSuperNode().getID());
//                content[tableRow][1]=Integer.toString(temp.getHierarchyLevel());
//                content[tableRow][2]=Integer.toString(temp.getGatewayIdUp());
//                Iterator<Integer> tIt=temp.getGatewayIdDown().iterator();
//                String stringTemp="";
//                while(tIt.hasNext()){
//                    stringTemp+=Integer.toString(tIt.next())+" ";
//                }
//                content[tableRow][3]=stringTemp;
//
//                Iterator<Node> mIt=temp.getMembers().iterator();
//                stringTemp="";
//                while(tIt.hasNext()){
//                    stringTemp+=Integer.toString(mIt.next().getID())+" ";
//                }
//                content[tableRow][4]=stringTemp;
//
//                content[tableRow][5]=Double.toString(temp.getTime().getSumTime());
//            }
//        }
//
//    }

    public void createTable(){
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JTable table = new JTable(this.content, this.titles);
        JScrollPane scrollPane = new JScrollPane(table);
        frame.add(scrollPane, BorderLayout.CENTER);
        frame.setSize(800, 600);
        frame.setVisible(true);
    }


}
